<script setup lang="ts">
import {onBeforeUpdate, onMounted, ref} from "vue";
import {storeToRefs} from "pinia";
import {useRouter, useRoute, onBeforeRouteUpdate} from "vue-router";
import useHospitalStore from "@/pinia/hospitalStore.ts";
import useUserStore from "@/pinia/userStore.ts";

const router = useRouter()
const route = useRoute()
const hospitalStore = useHospitalStore()
const userStore = useUserStore()

const {hospitalDetail, hospitalDeparment} = storeToRefs(hospitalStore)
const currentIndex = ref<number>(0)
const changeActive = (index: number) => {
  currentIndex.value = index
  const allTitleArray = document.querySelectorAll('.currentTitle')
  allTitleArray[index].scrollIntoView({
    behavior: 'smooth',
    block: 'start'
  })
  allTitleArray.forEach(item => {
    item.style.color = ''
  })
  allTitleArray[index].style.color = 'red'
}

const handleDepName = (deptarment: { depcode: string, children: null, depname: string }) => {
  const query = {hoscode: route.query.hoscode, depcode: deptarment.depcode}
  router.push({path: '/hospital/booking', query})
  // userStore.changeDialog(true)
}

const fetchHosDetail = async (hoscode) => {
  await hospitalStore.fetchHospitalDetail(hoscode)
}
onBeforeRouteUpdate((to, from, next) => {
  next()
  fetchHosDetail(to.query.hoscode)
})
</script>

<template>
  <div class="detail">
    <div class="top">
      <span class="hosname">{{ hospitalDetail.hospital?.hosname }}</span>
      <div class="level-info">
        <i class="iconfont icon-paiming"></i>
        <span class="level">{{ hospitalDetail.hospital?.param?.hostypeString }}</span>
      </div>
    </div>
    <div class="content">
      <div class="left">
        <el-avatar :size="90" :src="`data:image/jpeg;base64,${hospitalDetail?.hospital?.logoData}`">
          <img src="@/assets/images/logo.png"/>
        </el-avatar>
      </div>
      <div class="right">
        <div class="number">
          <div class="rules">挂号规则</div>
          <div class="time">预约周期: 10天 放号时间{{ hospitalDetail.bookingRule?.releaseTime }}: 停挂时间:{{ hospitalDetail.bookingRule?.quitTime }}</div>
          <div class="logout">退号时间: 就诊前一工作日15:30前取消</div>
          <div class="address">医院地址: {{ hospitalDetail.hospital?.param?.fullAddress }}</div>
          <div class="route">医院路线: {{ hospitalDetail.hospital?.route }}</div>
        </div>
        <div class="hos">
          <div class="rules">医院预约规则</div>
          <ul>
            <li v-for="(item,index) in hospitalDetail.bookingRule?.rule" :key="index">{{ item }}</li>
          </ul>
        </div>
      </div>
    </div>
    <div class="deparment-box">
      <ul class="leftNav">
        <li v-for="(dep,index) in hospitalDeparment" :key="dep.depcode" :class="{active:currentIndex===index}" @click="changeActive(index)">{{ dep.depname }}</li>
      </ul>
      <div class="deparmentInfo">
        <div class="showDeptment" v-for="(dep,index) in hospitalDeparment" :key="dep.depcode">
          <h1 class="currentTitle">{{ dep.depname }}</h1>
          <ul>
            <li v-for="(item,index) in dep.children" :key="item.depcode" @click="handleDepName(item)">{{ item.depname }}</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.detail {
  padding-top: 30px;
  padding-left: 30px;

  .top {
    display: flex;
    align-items: center;

    .hosname {
      font-size: 20px;
      font-weight: 700;
    }

    .level-info {
      margin-left: 30px;
      color: #7f7f7f;
    }
  }

  .content {
    padding-top: 50px;
    display: flex;

    .left {
      display: flex;
      align-items: center;

      .el-avatar {
        background-color: #ffffff;
        box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, .1);
        margin-right: 10px;
        cursor: pointer;
      }
    }

    .right {
      flex: 1;
      margin-top: 10px;
      padding-left: 20px;

      .number {
        margin-bottom: 20px;
      }

      .rules {
        color: #000;
        font-size: 16px;
      }

      div {
        color: #7f7f7f;
        margin-top: 10px;
        font-size: 14px;
      }

      ul {
        margin-top: 10px;
      }

    }
  }

  .deparment-box {
    display: flex;
    width: 100%;
    height: 800px;
    margin-top: 20px;

    .leftNav {
      width: 80px;
      height: 100%;
      background-color: rgb(248, 248, 248);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;


      li {
        width: 100%;
        flex: 1;
        color: #7f7f7f;
        font-size: 14px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        border-left: 2px solid rgb(248, 248, 248);

        &.active {
          background-color: #ffffff;
          border-left: 2px solid red;
          color: red;
        }

        &:hover {
          color: red;
        }
      }
    }

    .deparmentInfo {
      height: 100%;
      flex: 1;
      margin-left: 20px;
      overflow: auto;

      &::-webkit-scrollbar {
        display: none;
      }

      .showDeptment {
        margin: 10px 0;

        h1 {
          background-color: rgb(248, 248, 248);
          color: #7f7f7f;
          padding: 10px;
        }

        ul {
          display: flex;
          flex-wrap: wrap;

          li {
            width: 33.33%;
            line-height: 30px;
            color: #7f7f7f;
            padding: 10px;
            cursor: pointer;

            &:hover {
              color: red;

            }
          }
        }
      }

    }
  }
}
</style>
